package com.zcx.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;


/**
 * 项目名称：三农
 * 类描述：自定义的日志工具
 * 创建人：zcx
 * 创建时间：2017-08-25
 * 修改人：暂无
 * 修改时间：暂无
 * 修改备注：暂无
 */
public class LogUtil {

    public static final int VERBOSE = 1;
    public static final int DEBUG = 2;
    public static final int INFO = 3;
    public static final int WARN = 4;
    public static final int ERROR = 5;
    public static final int NOTHING = 6;
    public static final  int level = VERBOSE;

    public static void v(String tag, String msg) {
        if(level<=VERBOSE) {
            Log.v(tag,msg);
        }
    }
    public static void d(String tag, String msg) {
        if(level<=DEBUG) {
            Log.d(tag,msg);
        }
    }
    public static void i(String tag, String msg) {
        if(level<=INFO) {
            Log.i(tag,msg);
        }
    }
    public static void w(String tag, String msg) {
        if(level<=WARN) {
            Log.w(tag,msg);
        }
    }
    public static void e(String tag, String msg) {
        if(level<=ERROR) {
            Log.e(tag,msg);
        }
    }
    /**
     * 检查手机上是否安装了指定的软件
     * @param context
     * @param packageName：应用包名
     * @return
     */
    public static boolean isAvilible(Context context, String packageName){
        //获取packagemanager
        final PackageManager packageManager = context.getPackageManager();
        //获取所有已安装程序的包信息
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
        //用于存储所有已安装程序的包名
        List<String> packageNames = new ArrayList<String>();
        //从pinfo中将包名字逐一取出，压入pName list中
        if(packageInfos != null){
            for(int i = 0; i < packageInfos.size(); i++){
                String packName = packageInfos.get(i).packageName;
                packageNames.add(packName);
            }
        }
        //判断packageNames中是否有目标程序的包名，有TRUE，没有FALSE
        return packageNames.contains(packageName);
    }

}
